﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate.Mapping;

namespace NHibernate.Audit.Mapping
{
    public class AuditClassMetadata
    {

        public AuditClassMetadata(PersistentClass RealClass, AuditTableMetadata TableMetadata)
        {
            if (null == RealClass)
                throw new ArgumentNullException("RealClass");

            if (null == TableMetadata)
                throw new ArgumentNullException("TableMetadata");

            realClass = RealClass;
            properties = new Dictionary<string, AuditPropertyMetadata>();
            table = TableMetadata;
            table.ClassMetadata = this;
        }

        private PersistentClass realClass;
        private AuditTableMetadata table;
        private Dictionary<string, AuditPropertyMetadata> properties;

        public PersistentClass RealClass {
            get { return realClass; }
        }

        public string AuditClassName { get; set; } 

        public string AuditEntityName { get; set; }

        public AuditTableMetadata TableMetadata {
            get { return table; }
        }

        public void AddProperty(AuditPropertyMetadata metadata)
        {
            if (null == metadata)
                throw new ArgumentNullException("metadata");

            var property = metadata.Property;

            if (property.PersistentClass != realClass)
                throw new ArgumentException(string.Format("property must be a property of {0}, not {1}", realClass.ClassName, property.PersistentClass.ClassName));

            metadata.ClassMetadata = this;
            properties.Add(property.Name, metadata);
        }

        public IEnumerable<AuditPropertyMetadata> Properties
        {
            get { return properties.Values.AsEnumerable<AuditPropertyMetadata>(); }
        }

        public Property RevisionProperty { get; set; }

    }
}
